package candidate;

import com.dai.FlowableFirstApplication;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.*;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.idm.api.Group;
import org.flowable.task.api.Task;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.*;

/**
 * @ClassName: CanddidataGrouPeople
 * @Description: 类描述:候选人组
 * @date: 2025/7/17
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = FlowableFirstApplication.class)
@Slf4j
public class CanddidataGrouPeople {
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private ProcessEngine processEngine;//流程实例
    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private IdentityService identityService;


    @Test
    public void test() {
        System.out.println("我拿到了的是" + processEngine);
        log.info("CanddidataGrouPeople");
    }

    //流程部署
    @Test
    public void deployProcess() {
        Deployment deployed = repositoryService.createDeployment().name("候选人组流程案例").addClasspathResource("process/candidate/HolidatDemo2.bpmn20.xml").deploy();
        String id = deployed.getId();
        log.info("候选人组部署流程的id是" + id);
        //候选人组部署流程的id是7c741a5f-6312-11f0-ac7c-b025aa86e2c3
    }

    //删除部署流程
    @Test
    public void dele() {
        repositoryService.deleteDeployment("ddc20818-6311-11f0-9909-b025aa86e2c3", true);
    }

    //启动流程
    @Test
    public void startTask() {
        Map<String, Object> map = new HashMap<>();
        map.put("name", "xjy");

        ProcessInstance processInstance = runtimeService.startProcessInstanceById("HolidatDemo2:1:7c907c02-6312-11f0-ac7c-b025aa86e2c3", map);
        System.out.println("流程实例id是" + processInstance.getId());
    }

    //查询候选人组的代办
    @Test
    public void selectTask() {
        //查看当前登录用户的组
        Group group = identityService.createGroupQuery().groupMember("lisi").singleResult();
        System.err.println("目前我查出来的登录用户组是" + group.getId() + group.getName() + group.getType());
        List<Task> list = taskService.createTaskQuery().taskCandidateGroup(group.getId()).list();
        Iterator<Task> iterator = list.iterator();
        int i = 0;
        while (iterator.hasNext()) {

            i++;
            System.err.println("目前我查出来的候选人组的任务是" + iterator.next() + i);

        }
    }

    //拾取任务
    @Test
    public void claimTask() {
        //查看当前登录用户的组
        Group group = identityService.createGroupQuery().groupMember("lisi").singleResult();
        //查看当前登录用户候选组任务
        List<Task> list = taskService.createTaskQuery().taskCandidateGroup(group.getId()).list();
        for (Task task : list) {
            taskService.claim(task.getId(), "张三");
        }
    }
}
